package com.gdufe.studytogether.controller;

import com.gdufe.studytogether.domain.R;
import com.gdufe.studytogether.domain.pojo.Comment;
import com.gdufe.studytogether.domain.pojo.User;
import com.gdufe.studytogether.service.comment.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentService commentService;

    @GetMapping("/list")
    @CrossOrigin
    public R listComment(Long pageSize, Long pageCurrent, Integer cid, HttpServletRequest request,String comment){
        User user = (User) request.getSession().getAttribute("userLogin");
        return commentService.listComment(pageSize,pageCurrent,cid,user,comment);
    }

    @GetMapping("/getDetails")
    public R getCommentsByParentId(Long pageSize,Long pageCurrent,Integer id,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("userLogin");
        return commentService.getCommentsByParentId(pageSize,pageCurrent,id,user);
    }

    @PostMapping("/add")
    public R addComment(@RequestBody Comment comment) throws Exception {
        return commentService.addComment(comment);
    }

    @DeleteMapping("/delete")
    public R deleteComment(@RequestParam Integer id) throws Exception {
        return commentService.deleteComment(id);
    }

    @GetMapping("/like")
    public R likeComment(Integer id) throws Exception {
        return commentService.likeComment(id);
    }

    @GetMapping("/unlike")
    public R unlikeComment(Integer id) throws Exception {
        return commentService.unlikeComment(id);
    }
}
